package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_ACTIVITY_USER_RANK_CHANGE, desc = "跨服活动排行数据变化")
public class CrossActivityUserRankChangeCmd extends CrossCommand {

    private int activityId;

    private boolean isAll;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityRankChangeReqMsg reqMsg = CrossActivityProto.CrossActivityRankChangeReqMsg.parseFrom(packet.getBytes());
        isAll = reqMsg.getIsAll();
        activityId = reqMsg.getActivityId();
        for(RankProto.RankTempMsg temMsg : reqMsg.getDataListList()) {
            CrossUserRank crossUserRank = CrossRankPb.parseCrossUserRank(activityId, temMsg, eBigRankType.Normal.getValue());
            ActivityRankMgr.changeActivityRank(activityId, crossUserRank);
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        if(isAll){
            CrossActivityProto.CrossActivityRankChangeRespMsg.Builder respMsg = CrossActivityProto.CrossActivityRankChangeRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ACTIVITY_CHANGE_HAS_ALL_SYNC, respMsg);
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }
}
